/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import AdminOperations.AdminManageForum;
import CRUD.CategoryHome;
import CRUD.ForumHome;
import beans.AdminForumBean;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import xmlMapping.Category;
import xmlMapping.Forum;

/**
 *
 * @author Roaa
 */
public class AdminForumAction extends org.apache.struts.actions.DispatchAction {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    
    public ActionForward editForum(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int forumId = Integer.parseInt(request.getParameter("forumId"));
        List categories = new CategoryHome().getCategories();
        String[] categoriesNames = new String[categories.size()];
        Forum forum = new ForumHome().findById(forumId);
        for (int i=0 ; i<categories.size() ; i++) {
            categoriesNames[i] = ((Category) categories.get(i)).getName();
        }
        ((AdminForumBean) form).setCategories(categoriesNames);
        ((AdminForumBean) form).setCategory(forum.getCategory().getName());
        ((AdminForumBean) form).setForum(forum);
        return mapping.findForward("admEditForum");
    }

    public ActionForward saveForumChanges(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int forumId = ((AdminForumBean) form).getForumID();
        
        String name = ((AdminForumBean) form).getName();
        String desc = ((AdminForumBean) form).getDesc();
        Category cat = new CategoryHome().findByName(((AdminForumBean) form).getCategory());
        //Category cat = new CategoryHome().findById(3);
        Forum forum = new Forum(cat, name, desc);
        forum.setForumId(forumId);
        new AdminManageForum().updateForum(forum);
        return mapping.findForward("done");
    }

    public ActionForward addForum(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        List categories = new CategoryHome().getCategories();
        String[] categoriesNames = new String[categories.size()];
        for (int i=0 ; i<categories.size() ; i++) {
            categoriesNames[i] = ((Category) categories.get(i)).getName();
        }
        ((AdminForumBean) form).setCategories(categoriesNames);
        return mapping.findForward("admAddForum");
    }

    public ActionForward saveAddForum(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String name = ((AdminForumBean) form).getName();
        String desc = ((AdminForumBean) form).getDesc();
        Category cat = new CategoryHome().findByName(((AdminForumBean) form).getCategory());
        new AdminManageForum().addForum(new Forum(cat, name, desc));
        return mapping.findForward("done");
    }
}
